package com.kefu.chat.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.chat.mapper.ChatEvaluateMapper;
import com.kefu.chat.mapper.ChatEvaluateRelMapper;
import com.kefu.chat.model.ChatEvaluate;
import com.kefu.chat.model.ChatEvaluateRel;
import com.kefu.chat.vo.ChatEvaluateSearchVo;
import com.kefu.chat.vo.ChatEvaluateUpdateVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.common.util.JacksonUtil;
import com.kefu.common.util.StreamUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ChatEvaluateService  extends AbstractKefuService<ChatEvaluate>  {
	@Resource
    protected ChatEvaluateMapper chatEvaluateMapper;
	@Resource
	protected ChatEvaluateRelMapper chatEvaluateRelMapper;

	public ChatEvaluate saveOrUpdate(ChatEvaluateUpdateVo chatEvaluateVo) {
		String channelId = chatEvaluateVo.getChannelId();
		List<ChatEvaluate> list = chatEvaluateMapper.selectList(Wrappers.lambdaQuery(ChatEvaluate.class)
				.eq(ChatEvaluate::getChannelId, channelId));
		for (ChatEvaluate evaluate : list) {
			if (chatEvaluateVo.getId().equals(evaluate.getId())) {
				continue;
			}
			chatEvaluateMapper.deleteById(evaluate.getId());
			chatEvaluateRelMapper.delete(Wrappers.lambdaQuery(ChatEvaluateRel.class).eq(ChatEvaluateRel::getPid, evaluate.getId()));
			logger.info("删除非当前渠道绑定的满意度评价信息:{}, {}", channelId, evaluate);
		}
		ChatEvaluate info = chatEvaluateVo.convert();
		update(info);
		List<ChatEvaluateRel> starList = chatEvaluateVo.getStarList();
		chatEvaluateRelMapper.delete(Wrappers.lambdaQuery(ChatEvaluateRel.class).eq(ChatEvaluateRel::getPid, info.getId()));
		if (CollUtil.isNotEmpty(starList)) {
			starList.forEach(star -> {
				star.setPid(info.getId());
				chatEvaluateRelMapper.insert(star);
			});
		}
		List<ChatEvaluateRel> scoreList = chatEvaluateVo.getScoreList();
		if (CollUtil.isNotEmpty(scoreList)) {
			scoreList.forEach(score -> {
				score.setPid(info.getId());
				chatEvaluateRelMapper.insert(score);
			});
		}
		return info;
	}
	public ChatEvaluate getChatEvaluate(ChatEvaluateSearchVo search) {
		KefuContext kefuContext = KefuContext.getKefuContext();
		LambdaQueryWrapper<ChatEvaluate> wrapper = Wrappers.lambdaQuery(ChatEvaluate.class);
		wrapper.eq(ChatEvaluate::getChannelId, search.getChannelId());
		wrapper.last("limit 1");
		ChatEvaluate chatEvaluate = chatEvaluateMapper.selectOne(wrapper);
		if (Objects.isNull(chatEvaluate)) {
			chatEvaluate = new ChatEvaluate();
			chatEvaluate.setEvaluateAdminSolve(0);
			chatEvaluate.setDefaultMode(1);
			chatEvaluate.setChannelId(search.getChannelId());
			chatEvaluate.setCreateUser(kefuContext.getUserId());
			chatEvaluate.setCreateTime(kefuContext.getNow());
			chatEvaluate.setCreateName(kefuContext.getUserName());
			chatEvaluate.setUpdateUser(kefuContext.getUserId());
			chatEvaluate.setUpdateTime(kefuContext.getNow());
			chatEvaluate.setUpdateName(kefuContext.getUserName());
			save(chatEvaluate);
			for (int i= 1; i <= 5; i++) {
				ChatEvaluateRel chatEvaluateRel = new ChatEvaluateRel();
				chatEvaluateRel.setPid(chatEvaluate.getId());
				chatEvaluateRel.setLevelName(i + "星");
				chatEvaluateRel.setLevelType(1);
				chatEvaluateRel.setLevelValue(i);
				if (i == 5) {
					chatEvaluateRel.setLevelGuideMsg("非常满意，完美");
				}
				if (i == 4) {
					chatEvaluateRel.setLevelGuideMsg("满意，仍可改善");
				}
				if (i == 3) {
					chatEvaluateRel.setLevelGuideMsg("一般，还需改善");
				}
				if (i == 2) {
					chatEvaluateRel.setLevelGuideMsg("不满意，有点失望");
				}
				if (i == 1) {
					chatEvaluateRel.setLevelGuideMsg("非常不满意，特别失望");
				}
				chatEvaluateRel.setLevelTag("");
				chatEvaluateRel.setEvaluateGuideMsg("欢迎给我们服务提建议~");
				chatEvaluateRel.setLevelTagRequired(0);
				chatEvaluateRel.setEvaluateMsgRequired(0);
				chatEvaluateRelMapper.insert(chatEvaluateRel);
			}
			for (int i= 0; i <= 10; i++) {
				ChatEvaluateRel chatEvaluateRel = new ChatEvaluateRel();
				chatEvaluateRel.setId(IdUtil.simpleUUID());
				chatEvaluateRel.setPid(chatEvaluate.getId());
				chatEvaluateRel.setLevelName(i + "分");
				chatEvaluateRel.setLevelType(2);
				chatEvaluateRel.setLevelValue(i);
				if (i == 10 || i == 9) {
					chatEvaluateRel.setLevelGuideMsg("非常满意");
				}
				if (i == 8 || i == 7) {
					chatEvaluateRel.setLevelGuideMsg("满意，但需要改进");
				}
				if (i == 6 || i == 5 || i== 4 || i == 3 || i == 2) {
					chatEvaluateRel.setLevelGuideMsg("不满意");
				}
				if (i == 1 || i == 0) {
					chatEvaluateRel.setLevelGuideMsg("非常不满意");
				}
				chatEvaluateRel.setLevelTag("");
				chatEvaluateRel.setEvaluateGuideMsg("欢迎给我们服务提建议~");
				chatEvaluateRel.setLevelTagRequired(0);
				chatEvaluateRel.setEvaluateMsgRequired(0);
				chatEvaluateRelMapper.insert(chatEvaluateRel);
			}
		}
		List<ChatEvaluateRel> chatEvaluateRelList = this.chatEvaluateRelMapper.selectList(Wrappers.lambdaQuery(ChatEvaluateRel.class)
				.eq(ChatEvaluateRel::getPid, chatEvaluate.getId()));
		chatEvaluate.setStarList(
				StreamUtil.stream(chatEvaluateRelList).filter(item -> 1 == item.getLevelType())
						.sorted(Comparator.comparingInt(ChatEvaluateRel::getLevelValue).reversed()).collect(Collectors.toList())
		);
		chatEvaluate.setScoreList(
				StreamUtil.stream(chatEvaluateRelList).filter(item -> 2 == item.getLevelType())
						.sorted(Comparator.comparingInt(ChatEvaluateRel::getLevelValue).reversed()).collect(Collectors.toList())
		);
		logger.info("渠道评价配置:{}, {}", search.getChannelId(), JacksonUtil.toString(chatEvaluate));
		return chatEvaluate;
	}
}
